'use strict';
import React from 'react';
import Question from '../question/question';
import Layout from '../layout/layout';
import BodyFooter from '../layout/body.footer';

export default React.createClass({
    componentDidMount:function(){
    var self = this;
        $.ajax({
            url:'/api/questions',
            type:'get',
            data:{exam:1},
            dataType:'json',
            success:function(data){
                console.log(data);
                self.setState({questions:data,index:0});
            }
        });
    },
    componentWillMount:function(){
        // console.log('componentWillMount');
    },
    shouldComponentUpdate:function(nextProps,nextState){
        if(nextState.questions.length === 0){
            return false;
        }
        return true;
    },
    getDefaultProps:function(){
        return {};
    },
    componentWillReceiveProps:function(){
    //    console.log('componentWillReceiveProps');
    },
    componentWillUpdate:function(nextProps,nextState){
    //    console.log('componentWillUpdate');
    },
    componentDidUpdate:function(nextProps,nextState){
        // console.log('componentDidUpdate');
    },
    componentWillUnmount:function(){
        // console.log('componentWillUnmount');
    },
    preQuestion:function(){
        var curQuestion = this.refs.question.getCurValue();
        this.state.answers[this.state.index] = curQuestion;
        this.setState({index:this.state.index-1});
        console.log(this.state);
    },
    nextQuestion:function(){
        var curQuestion = this.refs.question.getCurValue();
        if(!curQuestion || !curQuestion.answer){
        // if(this.state.answers[this.state.index] === undefined || this.state.answers[this.state.index].answer == undefined){
            alert('请作答');
            return false;
        }
        this.state.answers[this.state.index] = curQuestion;
        // console.log(this.state.answers);
        console.log(this.state.answers[this.state.index+1]);
        this.state.answers[this.state.index+1] === undefined ? this.state.answers.push({}):'';
        var obj = {
            index : this.state.index+1,
            answers : this.state.answers
        };
        this.setState(obj);
        // console.log(this.state.answers);
    },
    answerQuestion : function(answer){
        this.state.answers[this.state.index] = _.assign(this.state.answers[this.state.index],answer);
    },
    getInitialState:function(){
        return {
            questions:[{
                title:'',
                index:0,
                res:[],
                type:''
            }],
            index:0,
            answers:[]
        }
    },
    render : function(){
        console.log('exam render .......');
        return (
            <div className="row">
            <Layout showType={this.state.questions[this.state.index].type}/>
        <Question ref='question' question={this.state.questions[this.state.index]} 
        curValue={this.state.answers[this.state.index]} />
        <BodyFooter cur={this.state.index} total={this.state.questions.length} nextQuestion={this.nextQuestion} preQuestion={this.preQuestion}/>
        </div>
        );
    }
});